<style lang="scss" scoped>
.nav_section {
  width: 100%;
}

.nav_section_items {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  padding: 15px;
  border-bottom: 1px solid #ddd;
  position: relative;
}

.nav_section_items:active {
  background: #ddd;
}

.nav_section_items .section_image {
  width: 125px;
  height: 80px;
}

.nav_section_items .section_image img {
  width: 100%;
  height: 100%;
}

.nav_section_items .section_cont {
  width: 200px;
  font-size: 13px;
  color: #a9a9a9;

}

.nav_section_items .section_cont div {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  display: block;
}

.nav_section_items .section_cont .section_cont_sub {
  font-size: 15px;
  line-height: 30px;
  color: #000;
  margin-bottom: 5px;
}

.nav_section_items .section_cont .section_cont_price {
  font-size: 16px;
  color: #d24a58;
}
</style>

<template>
  <div class="container hasNav">
    <van-nav-bar title="预约服务" left-arrow left-text="返回" @click-left="$router.back()" fixed />
    <div class="nav_section">
      <div class="nav_section_items">
        <div class="section_image">
          <img :src="picUrl" />
        </div>
        <div class="section_cont">
          <div class="section_cont_sub">
            <span>{{ serviceName }}</span>
          </div>
          <div class="section_cont_price">价格：￥{{ linePrice }}</div>
        </div>
      </div>
    </div>
    <van-form>
      <van-field readonly :value="reservationStatus" label="预约状态" />
      <van-field readonly :value="reservationDate" label="预约日期" />
      <van-field readonly :value="reservationTime" label="预约时间" />
      <van-field readonly v-model="note" rows="3" autosize label="预约留言" type="textarea" />
      <div v-if="reservationStatus == '预约中'" style="margin: 10px; width: 43%; float: left;">
        <van-button block type="default" color="#07C160" @click="confirmFinishReservation()">完成预约</van-button>
      </div>
      <div v-if="reservationStatus == '预约中'" style="margin: 10px; width: 43%; float: right;">
        <van-button block type="default" color="#d24a58" @click="confirmCancelReservation()">取消预约</van-button>
      </div>
    </van-form>
    <Loading :isShow="loading"></Loading>
  </div>
</template>

<script>
import Loading from '@/components/Loading';
import { Dialog } from 'vant';
import { fileUrl, getMyReservationDetail, finishReservation, cancelReservation } from '../../api/reservation';

export default {
  name: 'myReservationDetail',
  components: {
    Loading,
    [Dialog.Component.name]: Dialog.Component,
  },
  data() {
    return {
      fileApi: fileUrl,
      reservationId: null,
      loading: true,
      serviceName: '',
      picUrl: '',
      linePrice: 0.00,
      reservationDate: null,
      reservationTime: null,
      note: null,
      reservationStatus: '预约中',
    };
  },
  mounted() {
    let id = this.$route.query.id;
    this.reservationId = id;
    this.getMyReservationDetail(id);
  },
  methods: {

    async getMyReservationDetail(id) {
      try {
        this.loading = true;
        const { code, message, data } = await getMyReservationDetail({ id: id });
        if (code == 200) {
          this.serviceName = data.serviceName;
          this.picUrl = fileUrl + data.picUrl;
          this.linePrice = data.linePrice;
          this.reservationDate = data.reservationDate;
          this.reservationTime = data.reservationTime;
          this.note = data.note;
          if (data.status == 1) {
            this.reservationStatus = "已完成";
          } else if (data.status == 2) {
            this.reservationStatus = "已取消";
          }
        } else {
          this.$toast(message);
        }
        this.$nextTick(() => {
          this.loading = false;
        });
      } catch (error) {
        console.log(error);
        this.loading = false;
      }
    },

    confirmFinishReservation() {
      Dialog.confirm({
        message: '确认是否已完成服务?',
      }).then(() => {
        this.finishReservation(this.reservationId)
      }).catch(() => {
        // on cancel
      });
    },

    async finishReservation(id) {
      try {
        const { code, message } = await finishReservation({ id: id });
        if (code == 200) {
          this.getMyReservationDetail(this.reservationId);
          this.$toast('预约已完成')
        } else {
          this.$toast(message);
        }
      } catch (error) {
        console.log(error);
      }
    },
    
    confirmCancelReservation() {
      Dialog.confirm({
        message: '预约取消后无法进行服务！',
      }).then(() => {
        this.cancelReservation(this.reservationId)
      }).catch(() => {
        // on cancel
      });
    },

    async cancelReservation(id) {
      try {
        const { code, message } = await cancelReservation({ id: id });
        if (code == 200) {
          this.getMyReservationDetail(this.reservationId);
          this.$toast('预约已取消')
        } else {
          this.$toast(message);
        }
      } catch (error) {
        console.log(error);
      }
    },

  }
};
</script>